import axios from 'axios';

import type {
  CardListResult,
  GoodsListResult,
  ListResult,
  newItem,
  newService,
  OrderDetail,
  OrderListResult,
  ProductDetail,
} from '@/api/model/listModel';
import { request } from '@/utils/request';

const Api = {
  BaseList: '/get-list',
  CardList: '/get-card-list',
  ProductCardList: '/user/getItems',
  ProductDetail: '/get-product-detail',
  GoodsList: '/admin/getAllInfo',
  ItemList: '/api/admin/getAllItem',
  ServiceList: '/api/admin/getAllService',
  AddItem: '/admin/addItem',
  AddService: '/api/admin/addService',
  UserBuy: '/api/user/buy',
  // 删除Item和Service的接口使用同一个，别问我为什么叫DeleteItem.
  DeleteGoods: '/api/admin/deleteItem',
  UserBuy: '/user/buy',
  DeleteGoods: '/api/admin/deleteGoods',
  GoodsDetail: '/api/user/getItemInfo',
  OrderList: '/admin/getOrderInfo',
  OrderDetail: '/api/admin/getOrderDetail',
  DeleteOrder: '/api/admin/deleteOrder',
  ModifyOrderStatus: '/api/admin/modifyOrderStatus',
  GetCart: '/api/user/getCart',
};

export function getList() {
  return request.get<ListResult>({
    url: Api.BaseList,
  });
}
export function getCart() {
  return request.get<OrderListResult>({
    url: Api.GetCart,
  });
}
export function getCardList() {
  return request.get<CardListResult>({
    url: Api.CardList,
    params: {
      type: 'service',
    },
  });
}

export function getProductCardList() {
  return request.get<CardListResult>({
    url: Api.ProductCardList,
  });
}
export function getServiceCardList() {
  return request.get<CardListResult>({
    url: Api.ProductCardList,
    params: {
      type: 'service',
    },
  });
}
export function getGoodsCardList() {
  return request.get<CardListResult>({
    url: Api.ProductCardList,
    params: {
      type: 'service',
    },
  });
}
export function getProductDetail() {
  return request.get<ProductDetail>({
    url: Api.ProductCardList,
  });
}

// 若无参数(name)传入，则查询所有的商品，有则查询指定的商品
export function getGoodsList(name: String) {
  return request.get({
    url: Api.GoodsList,
    data: name,
  });
}

// 获取所有的Item类商品
export function getItemList() {
  return request.get<GoodsListResult>({
    url: Api.ItemList,
  });
}

// 获取所有的Service类商品
export function getServiceList() {
  return request.get<GoodsListResult>({
    url: Api.ServiceList,
  });
}

export function addItem(item: newItem) {
  request.delete<boolean>({
    url: Api.AddItem,
    data: item,
  });
}

export function addService(service: newService) {
  request.post<boolean>({
    url: Api.AddService,
    data: service,
  });
}
// 更新

// 用户购买
export function userBuy(id: number) {
  return request.post<boolean>({
    url: Api.UserBuy,
    data: id,
  });
}
// 管理员删除
export function deleteGoods(id: number) {
  return request.post<boolean>({
    url: Api.DeleteGoods,
    data: id,
  });
}
// 详情页
export function getGoodsDetail(id: number) {
  return request.get<GoodsListResult>({
    url: Api.GoodsDetail,
    data: id,
  });
}

export function getOrdersList(name: String, status: number, id: number) {
  return request.get<OrderListResult>({
    url: Api.OrderList,
    data: {
      name,
      status,
      id,
    },
  });
}

export function getOrderDetail(id: number) {
  return request.get<OrderDetail>({
    url: Api.OrderDetail,
    data: id,
  });
}

export function deleteOrder(id: number) {
  request.post<boolean>({
    url: Api.DeleteOrder,
    data: id,
  });
}

export function modifyOrderStatus(id: number, status: number) {
  request.post<boolean>({
    url: Api.ModifyOrderStatus,
    data: {
      id,
      status,
    },
  });
}
